using System;
using System.Collections.Generic;
using BMCommon;
using Data;
using GTA;
using Holoville.HOTween;
using SevenPieceGame;
using TMPro;
using UnityEngine;

public class ClearPopupHandler : MonoBehaviour, IPopupWnd, IHandlerBase
{
	public void DoDataExchange()
	{
	}

	public void OnEnterPopup(object param)
	{
		BMSoundHelper.Play("se_clearpopup", false, 1f);

		int _isFirst = PlayerPrefsManager.GetInt("85t0rk",0);
		if (_isFirst == 0)
		{
			AdjustWrapper.Instance.LogEvent("85t0rk", null);
			PlayerPrefsManager.SetInt("85t0rk",1);
		}
		
		int _count = PlayerPrefsManager.GetInt(PlayerPrefsManager.SAVE_STAGE_PIECE_COUNT, 0);
		_count += MainManager.Instance.m_CurTempPieceCount;
		PlayerPrefsManager.SetInt(PlayerPrefsManager.SAVE_STAGE_PIECE_COUNT, _count);
		
		this.collection.SetActive(false);
		object[] array = (object[])param;
		this.boardInfos = (List<BoardInfo>)array[0];
		this.isFirstClear = (bool)array[1];
		this.currentlevel = (Level)array[2];
		this.isPackageClear = (bool)array[3];
		this.boardBg.transform.localPosition = new Vector3(0f, (this.top.transform.localPosition.y + this.bottom.transform.localPosition.y) / 2f, this.boardBg.transform.localPosition.z);
		this.LoadLevel();
		ClearPopupHandler.closeMessage = "Next";
		this.title.text = this.randomTitle[UnityEngine.Random.Range(0, this.randomTitle.Length)];
		if (DataContainer<Data.Root>.Data.currentPackage.record.isLocked && DataContainer<Data.Root>.Data.currentLevel.num >= 4)
		{
			this.btnNext.text = "OK";
			this.isTrialClear = true;
		}
		else
		{
			this.btnNext.text = ((DataContainer<Data.Root>.Data.currentPackage.levels.Length != DataContainer<Data.Root>.Data.currentLevel.num + 1) ? "Next" : "OK");
			this.isTrialClear = false;
		}
		this.btnRoot.gameObject.transform.localScale = Vector3.zero;
		this.board.gameObject.transform.localScale = Vector3.zero;
		this.userLevel.gameObject.transform.localScale = Vector3.zero;
		this.giftBox.SetActive(true);
		this.collectionBg.color = new Color(0f, 0f, 0f, 0f);
		this.collectionTextRoot.gameObject.transform.localScale = Vector3.zero;
		this.collectionBtn.gameObject.transform.localScale = Vector3.zero;
		this.ClearPopupAnimation();
	}

	public void OnLeavePopup()
	{
		this.ClearBoxWidget();
		this.ClearHinderWidget();
		this.ClearBoardWidget();
	}

	private void LoadLevel()
	{
		this.ClearBoxWidget();
		this.ClearHinderWidget();
		this.ClearBoardWidget();
		float num = this.top.transform.position.y - this.bottom.transform.position.y;
		float num2 = 0f;
		if (GameMode.gameMode == GameType.HEXA || GameMode.gameMode == GameType.RHOMBUS)
		{
			num2 = BlocksCoordinate.coordinate.coordinate.SizeY / 2f;
		}
		BlocksCoordinate.coordinate.coordinate.size = BlocksCoordinate.coordinate.defaultCoordinate.size;
		if (BlocksCoordinate.coordinate.coordinate.SizeY * (float)DataContainer<Data.Root>.Data.currentLevel.height + num2 > num)
		{
			BlocksCoordinate.coordinate.coordinate.size = num / (float)DataContainer<Data.Root>.Data.currentLevel.height / (BlocksCoordinate.coordinate.coordinate.SizeY / BlocksCoordinate.coordinate.coordinate.size) - num2 / (float)DataContainer<Data.Root>.Data.currentLevel.height;
		}
		float num3 = (float)DataContainer<Data.Root>.Data.currentLevel.width;
		if (GameMode.gameMode == GameType.TANGRAM)
		{
			num3 += 2f;
		}
		else if (GameMode.gameMode == GameType.TRIANGLE)
		{
			num3 += 1f;
		}
		else if (GameMode.gameMode == GameType.RHOMBUS)
		{
			num3 *= 0.6666667f;
		}
		float num4 = Mathf.Abs(this.left.transform.localPosition.x) * 2f;
		if (BlocksCoordinate.coordinate.coordinate.SizeX * num3 > num4)
		{
			BlocksCoordinate.coordinate.coordinate.size = num4 / num3 / (BlocksCoordinate.coordinate.coordinate.SizeX / BlocksCoordinate.coordinate.coordinate.size);
		}
		for (int i = 0; i < this.boardInfos.Count; i++)
		{
			Vector3 localPosition = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)this.boardInfos[i].point.x, (float)this.boardInfos[i].point.y);
			localPosition.z = -1f;
			BoardWidgetHandler item = Widget.Create<BoardWidgetHandler>();
			this.boardWidgets.Add(item);
			this.boardWidgets[i].transform.parent = this.boardBg.transform;
			this.boardWidgets[i].transform.localPosition = localPosition;
			this.boardWidgets[i].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
			this.boardWidgets[i].SetData(this.boardInfos[i].isUp, this.boardInfos[i].point, this.currentlevel.editMinPoint, -1, false);
			this.boardWidgets[i].name = "board";
			BoxWidgetHandler item2 = Widget.Create<BoxWidgetHandler>();
			this.boxWidgets.Add(item2);
			localPosition.z = -2f;
			this.boxWidgets[i].transform.parent = this.boardBg.transform;
			this.boxWidgets[i].transform.localPosition = localPosition;
			this.boxWidgets[i].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
			this.boxWidgets[i].tag = "Untagged";
			int index = (this.boardInfos[i].changeColorIndex <= -1 || this.boardInfos[i].changeColorIndex >= 255) ? this.boardInfos[i].colorIndex : this.boardInfos[i].changeColorIndex;
			this.boxWidgets[i].SetData(index, this.boardInfos[i].isUp, this.boardInfos[i].blockTangramIndex, BlockType.NORMAL);
			this.boxWidgets[i].name = "box";
			this.boxWidgets[i].SetOutline(false);
		}
		if (this.currentlevel.hinder != null)
		{
			for (int j = 0; j < this.currentlevel.hinder.Length; j++)
			{
				Point point = BlocksCoordinate.coordinate.coordinate.IndexToPuzzle((int)this.currentlevel.hinder[j]);
				bool isUp;
				if (this.currentlevel.isReverse)
				{
					isUp = ((point.x + point.y) % 2 != 0);
				}
				else
				{
					isUp = ((point.x + point.y) % 2 == 0);
				}
				int tangramIndex = (point.x + (int)this.currentlevel.editMinPoint[0] + (((point.y + (int)this.currentlevel.editMinPoint[1]) % 2 != 0) ? 2 : 0)) % 4;
				Vector3 localPosition2 = BlocksCoordinate.coordinate.coordinate.PuzzleToWorld((float)point.x, (float)point.y);
				localPosition2.z = -2f;
				HinderWidgetHandler item3 = Widget.Create<HinderWidgetHandler>();
				this.hinderWidgets.Add(item3);
				this.hinderWidgets[j].transform.parent = this.boardBg.transform;
				this.hinderWidgets[j].transform.localPosition = localPosition2;
				this.hinderWidgets[j].transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				this.hinderWidgets[j].name = "hinder";
				this.hinderWidgets[j].SetData(isUp, tangramIndex);
				localPosition2.z = -1f;
				BoardWidgetHandler boardWidgetHandler = Widget.Create<BoardWidgetHandler>();
				boardWidgetHandler.transform.parent = this.boardBg.transform;
				boardWidgetHandler.transform.localPosition = localPosition2;
				boardWidgetHandler.transform.localScale = Vector3.one * BlocksCoordinate.coordinate.coordinate.size;
				boardWidgetHandler.SetData(isUp, point, this.currentlevel.editMinPoint, -1, false);
				boardWidgetHandler.name = "board";
				this.boardWidgets.Add(boardWidgetHandler);
			}
		}
		this.GeneratePosition();
	}

	private void GeneratePosition()
	{
		float num = 9999f;
		float num2 = -9999f;
		for (int i = 0; i < this.boardWidgets.Count; i++)
		{
			if (this.boardWidgets[i].transform.position.y < num)
			{
				num = this.boardWidgets[i].transform.position.y;
			}
			if (this.boardWidgets[i].transform.position.y > num2)
			{
				num2 = this.boardWidgets[i].transform.position.y;
			}
		}
		for (int j = 0; j < this.hinderWidgets.Count; j++)
		{
			if (this.hinderWidgets[j].transform.position.y < num)
			{
				num = this.hinderWidgets[j].transform.position.y;
			}
			if (this.hinderWidgets[j].transform.position.y > num2)
			{
				num2 = this.hinderWidgets[j].transform.position.y;
			}
		}
		Vector3 position = this.boardBg.transform.position;
		float num3 = (num - position.y + (num2 - position.y)) / 2f;
		this.boardBg.transform.localPosition = new Vector3(this.boardBg.transform.localPosition.x, this.boardBg.transform.localPosition.y - num3, this.boardBg.transform.localPosition.z);
	}

	private void ClearBoxWidget()
	{
		for (int i = 0; i < this.boxWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.boxWidgets[i].gameObject);
		}
		this.boxWidgets.Clear();
	}

	private void ClearHinderWidget()
	{
		for (int i = 0; i < this.hinderWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.hinderWidgets[i].gameObject);
		}
		this.hinderWidgets.Clear();
	}

	private void ClearBoardWidget()
	{
		for (int i = 0; i < this.boardWidgets.Count; i++)
		{
			UnityEngine.Object.Destroy(this.boardWidgets[i].gameObject);
		}
		this.boardWidgets.Clear();
	}

	private void ClearPopupAnimation()
	{
		this.skip.SetActive(true);
		this.isSkip = false;
		Sequence sequence = new Sequence(new SequenceParms().Id("ClearPopupAnimation").OnComplete(delegate()
		{
			this.skip.SetActive(false);
			if (this.isFirstClear)
			{
				if (!string.IsNullOrEmpty(this.currentlevel.pictureTheme))
				{
					this.currentlevel.record.isNewCollection = true;
					this.currentlevel.SaveContext();
					this.CollectionAnimation();
				}
				else
				{
					this.SetClearExp();
				}
			}
			else
			{
				this.ShowPackageClearPopup();
			}
		}));
		sequence.Insert(0f, this.board.gameObject.Popup(0.5f));
		sequence.Insert(0.3f, this.userLevel.gameObject.Popup(0.5f));
		sequence.Play();
	}

	private void StopClearPopupAnimation()
	{
		this.isSkip = true;
		HOTween.Complete("ClearPopupAnimation");
	}

	private void CollectionAnimation()
	{
		this.pictureName.text = StringExt.UppercaseFirst(this.currentlevel.pictureName);
		this.collection.SetActive(true);
		Sequence sequence = new Sequence(new SequenceParms().Id("CollectionAnimation").OnComplete(delegate()
		{
		}));
		sequence.Insert(0f, this.collectionTextRoot.gameObject.Popup(0.5f));
		sequence.Insert(0.3f, HOTween.To(this.collectionBtn.gameObject.transform, 0.5f, new TweenParms().Prop("localScale", new Vector3(1f, 1f, 1f)).Ease(EaseType.EaseOutBack)));
		sequence.Insert(0f, HOTween.To(this.collectionBg, 0.5f, new TweenParms().Prop("color", new Color(0f, 0f, 0f, 0.95f))));
		sequence.Play();
	}

	private void StopCollectionAnimation()
	{
		HOTween.Complete("CollectionAnimation");
	}

	private void SetClearExp()
	{
		this.userLevel.SetClearExp(delegate(bool isLevelUp)
		{
			if (isLevelUp)
			{
				this.giftBox.SetActive(false);
				Vector3 vector = this.giftBox.transform.position - base.transform.position;
				Scene.ShowPopup("LevelUpRewardPopup", vector, delegate(object isClose)
				{
					this.ShowPackageClearPopup();
					this.giftBox.SetActive(true);
				});
			}
			else
			{
				this.ShowPackageClearPopup();
			}
		}, this.isSkip);
	}

	private void ShowPackageClearPopup()
	{
		if (this.isPackageClear)
		{
			Scene.ShowPopup("PackageClearPopup", null, delegate(object isOk)
			{
				if ((bool)isOk)
				{
					ClearPopupHandler.closeMessage = "Next";
				}
				else
				{
					ClearPopupHandler.closeMessage = "NextPackage";
				}
				this.PopupClose();
			});
		}
		else if (this.isTrialClear)
		{
			Scene.ShowPopup("IapUnLockPackagePopup", true, delegate(object message)
			{
				if (!DataContainer<Data.Root>.Data.currentPackage.record.isLocked)
				{
					ClearPopupHandler.closeMessage = "Next";
					this.btnNext.text = "Next";
					if (!this.isSkip)
					{
						HOTween.To(this.btnRoot.gameObject.transform, 0.5f, new TweenParms().Prop("localScale", new Vector3(1f, 1f, 1f)).Ease(EaseType.EaseOutBack));
					}
					else
					{
						this.btnRoot.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
					}
				}
				else
				{
					this.PopupClose();
				}
			});
		}
		else if (!this.isSkip)
		{
			HOTween.To(this.btnRoot.gameObject.transform, 0.5f, new TweenParms().Prop("localScale", new Vector3(1f, 1f, 1f)).Ease(EaseType.EaseOutBack));
		}
		else
		{
			this.btnRoot.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		}
	}

	private void OnClickNext()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		ClearPopupHandler.closeMessage = "Next";
		this.PopupClose();
	}

	private void OnClickBack()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		ClearPopupHandler.closeMessage = "Back";
		this.PopupClose();
	}

	private void OnClickCollectionDone()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.collection.SetActive(false);
		this.SetClearExp();
	}

	private void OnClickCollection()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		DWATTHelper.logEvent("Collection", new object[]
		{
			"ClearPopupEnter"
		});
		BlockPopupTransition.PopupAnimation(delegate
		{
			Scene.ShowPopup("CollectionPopup", this.currentlevel.pictureTheme, null);
		});
	}

	private void OnEscape()
	{
		if (!this.skip.activeSelf)
		{
			ClearPopupHandler.closeMessage = "Next";
			this.PopupClose();
		}
	}

	private void PopupClose()
	{
		Scene.ClosePopup("ClearPopup", true);
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public static string closeMessage;

	private string[] randomTitle = new string[]
	{
		"AWESOME!",
		"GREAT!",
		"AMAZING!",
		"FANTASTIC!",
		"WONDERFUL!",
		"MARVELOUS!",
		"STUNNING!",
		"BRILLIANT!"
	};

	private bool isFirstClear;

	private List<BoardInfo> boardInfos;

	[HideInInspector]
	public float animationPercent;

	public TopOverlayHandler topOverlay;

	public GameObject boardBg;

	public GameObject top;

	public GameObject bottom;

	public GameObject left;

	public GameObject board;

	public GameObject skip;

	public GameObject btnRoot;

	public TextMeshPro title;

	public TextMeshPro btnNext;

	public UserLevel userLevel;

	public GameObject coloredPaper;

	public GameObject giftBox;

	public GameObject collection;

	public TextMeshPro pictureName;

	public GameObject collectionTextRoot;

	public tk2dSprite collectionBg;

	public GameObject collectionBtn;

	private Level currentlevel;

	private bool isPackageClear;

	private bool isTrialClear;

	private List<BoardWidgetHandler> boardWidgets = new List<BoardWidgetHandler>();

	private List<HinderWidgetHandler> hinderWidgets = new List<HinderWidgetHandler>();

	private List<BoxWidgetHandler> boxWidgets = new List<BoxWidgetHandler>();

	private bool isSkip;
}
